/*
* @(#)EmailMessageComparatorFactory.java
*
* Copyright (c) 2004 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.mail.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * EmailMessageComparatorFactory.java
 *
 * Factory class for creating EmailMessageComparator object based on the sorting attribute.
 * The default sorting is {@link EmailMessageComparator#SORT_BY_SEND_DATE} {@link EmailMessageComparator#SORT_DESC}
 *
 * @author      Lun Au
 * @company     DCIVision Limited
 * @creation    28/03/2007
 * @version     $Revision: 1.1 $
 */
public class EmailMessageComparatorFactory {

    /**
     * The logger
     */
    protected static Log log = LogFactory.getLog((new EmailMessageComparatorFactory()).getClass().getName());

    /**
     * Private constructor forbidens creating from outside
     */
    private EmailMessageComparatorFactory(){
    }

    /**
     * Returns the correct comparator object based on sorting attribute and order.<p>
     * 
     * @param sortName The attribute of an email to be compared. Values include
     *                 {@link EmailMessageComparator#SORT_BY_PRIORITY},
     *                 {@link EmailMessageComparator#SORT_BY_SUBJECT},
     *                 {@link EmailMessageComparator#SORT_BY_SENDER},
     *                 {@link EmailMessageComparator#SORT_BY_SEND_DATE},
     *                 {@link EmailMessageComparator#SORT_BY_SIZE}
     * @param sortOrder The sorting order, ascendingly or descendingly. Values include
     *                 {@link EmailMessageComparator#SORT_ASC},
     *                 {@link EmailMessageComparator#SORT_DESC}
     * @return A correct comparator object
     */
    public static EmailMessageComparator getInstance (String sortName, String sortOrder) {
        if ( EmailMessageComparator.SORT_BY_PRIORITY.equals(sortName) ) {
            return new EmailMessagePriorityComparator(sortOrder);
        } else if (EmailMessageComparator.SORT_BY_SUBJECT.equals(sortName) ) {
            return new EmailMessageSubjectComparator(sortOrder);
        } else if (EmailMessageComparator.SORT_BY_SENDER.equals(sortName) ) {
            return new EmailMessageSenderComparator(sortOrder);
        } else if (EmailMessageComparator.SORT_BY_SEND_DATE.equals(sortName) ) {
            return new EmailMessageSentDateComparator(sortOrder);
        } else if (EmailMessageComparator.SORT_BY_SIZE.equals(sortName) ) {
            return new EmailMessageSizeComparator(sortOrder);
        } else {
log.warn("Can not found suitable EmailMessageComparator, using SentDate as default");
            return new EmailMessageSentDateComparator(sortOrder);
        }
    }
}